<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" xmlns="http://www.w3.org/1999/html">
<head th:include="/include/commonHeader::commonHeader('人员备份添加')"></head>
<body>
<div class="wrapper wrapper-content">
    <div class="col-sm-12">
        <form class="form-horizontal m-t" id="exampleForm">
            <input id="flightDate" name="flightDate"  type="hidden">
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label><font style="color:red;">*</font>备份开始：</label>
                </div>
                <div class="col-xs-8">
                    <input id="startTime" name="startTime" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label><font style="color:red;">*</font>备份结束：</label>
                </div>
                <div class="col-xs-8">
                    <input id="endTime" name="endTime" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label><font style="color:red;">*</font>备份类型：</label>
                </div>
                <div class="col-xs-8">
                    <select id="bakType" name="bakType" class="form-control"></select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label><font style="color:red;">*</font>备份人：</label>
                </div>
                <div class="col-xs-8">
                    <select id="pCode" name="pCode" class="form-control"></select>
                </div>
            </div>
            <div class="form-group">
                <div class="text-center">
                    <button id="btn_save" onclick="save()" type="button" class="btn btn-primary">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="/plugins/bootstrapValidator/js/bootstrapValidator.min.js"></script>
<script src="/plugins/bootstrapValidator/js/language/zh_CN.js"></script>
<!-- 自定义js -->
<script th:inline="javascript">
    var prefix = "/crewBackup";
    var moduleFlag = [[${moduleFlag}]];
    $(function () {
        initPlugin();
        initValidator();
    });

    function initPlugin() {
        var paramArr = [{'moduleFlag':moduleFlag}];
        select2CommonAsync("#pCode","/select2/getStaffPCode.do","人员姓名/首字母",null,paramArr);
        //select2InitForData("#pCode", "/select2/getCrewPCode.do?moduleFlag=" + moduleFlag, null, null, false, null);
        //select2InitialForCNamePCode("#pCode", null, moduleFlag,null);
        select2InitForData("#bakType", "/backupType/getSelect2.do?moduleFlag=" + moduleFlag, null, null, false, null);

        $("#startTime").datetimepicker({
            minView: "hour",
            language: 'zh-CN',
            format: 'yyyy-mm-dd hh:ii:00',
            autoclose: true,
            todayBtn: true,
        }).on('changeDate show', function (ev) {
            $("#endTime").datetimepicker('setStartDate', $("#startTime").val());
            $('#exampleForm').data('bootstrapValidator')
                .updateStatus('startTime', 'NOT_VALIDATED', null)
                .validateField('startTime');
        }).on('change',function () {
            var hours = "23:59:00";
            if($("#endTime").val()!=null && $("#endTime").val()!=''){
                hours = $("#endTime").val().split(" ")[1];
            }
            var day = $("#startTime").val().split(" ")[0];
            $("#endTime").val(day+" "+hours);
        });

        $("#endTime").datetimepicker({
            minView: "hour",
            language: 'zh-CN',
            format: 'yyyy-mm-dd hh:ii:00',
            autoclose: true,
            todayBtn: true,
        }).on('changeDate show', function (ev) {
            $("#startTime").datetimepicker('setEndDate', $("#endTime").val());
            $('#exampleForm').data('bootstrapValidator')
                .updateStatus('endTime', 'NOT_VALIDATED', null)
                .validateField('endTime');
        }).on('change',function () {
            var hours = "00:00:00";
            if($("#startTime").val()!=null && $("#startTime").val()!=''){
                hours = $("#startTime").val().split(" ")[1];
            }
            var day = $("#endTime").val().split(" ")[0];
            $("#startTime").val(day+" "+hours);
        });

        $("#bakType").change(function () {
            $("#exampleForm").data('bootstrapValidator')
                .updateStatus('bakType', 'NOT_VALIDATED')
                .validateField('bakType')
                .updateStatus('flightDate', 'NOT_VALIDATED')
                .validateField('flightDate');
        });
    }


    //保存
    function save() {
        $("#exampleForm").data('bootstrapValidator').validate();//触发一次所有的验证
        if (!$("#exampleForm").data('bootstrapValidator').isValid()) {//判断验证是否已经通过
            return;
        }
        if($("#startTime").val() != null){
            var flightDate = $("#startTime").val().split(" ")[0];
            $("#flightDate").val(flightDate);
        }

        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/save.do",
            data: $('#exampleForm').serialize(),
            error : function(request) {
                toastr.error("Connection error");
            },
            success : function(data) {
                if (data.code == 0) {
                    parent.toastr.success("操作成功");
                    parent.reload();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                } else {
                    toastr.warning(data.msg)
                }
            }
        });
    }
    function change(){
        $("#exampleForm").data('bootstrapValidator')
            .updateStatus('startTime', 'NOT_VALIDATED')
            .validateField('startTime')
            .updateStatus('endTime', 'NOT_VALIDATED')
            .validateField('endTime')
            .updateStatus('pCode', 'NOT_VALIDATED')
            .validateField('pCode');
    }
    //初始化验证规则
    function initValidator() {
        $('#exampleForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                flightDate: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '日期不能为空'
                        },
                        remote: {
                            delay: 1,
                            message: '同一天同一备份人不能重复',
                            url: '/crewBackup/check.do',
                            data: function (validator) {
                                return {
                                    pCode: $("#pCode").val(),
                                    moduleFlag: moduleFlag
                                };
                            }
                        }
                    }
                },
                bakType: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '备份类型不能为空'
                        }
                    }
                },
                pCode: {
                    message: 'The value is not valid',
                    validators: {
                        notEmpty: {
                            message: '备份人不能为空'
                        },
                        remote: {
                            delay: 1,
                            message: '同一天同一类型不能重复',
                            url: '/crewBackup/check.do',
                            data: function (validator) {
                                return {
                                    flightDate: $("#flightDate").val(),
                                    moduleFlag: moduleFlag
                                };
                            }
                        }
                    }
                }
            }
        });
    }
</script>
</body>
</html>
